package edu.buffalo.cse.cse486_586.util;

import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;

import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class AppUtil {
	public final static String TAG = "APPUTIL";

	//totally for testing
	public final static int numDev = 5;
	//message status
	public static final int UNDECIDED = 1;
	public static final int DELIVERABLE= 2;
	public static final int PRINTABLE = 3;
	// messager state
	public static final int MSG_REGISTER_CLIENT = 1;
	public static final int MSG_UNREGISTER_CLIENT = 2;
	public static final int MSG_SEND = 3;
	public static final int MSG_RECEIVE = 4;
	public static final int MSG_DELIVER = 5;
	public static final int TEST_CASE_1 = 6;
	public static final int TEST_CASE_2 = 7;
	// data bundle key
	public static final String FOREWARD_FALG = "foreward";
	public static final String LOCAL_IP = "local_ip";
	public static final String EMULATOR_IP = "10.0.2.2";
	public static final String IP = "ip";
	public static final String SEND_PORT = "port";
	public static final String CONTENT = "content";
	public static final String TOTOAL_ORDER = "total_order";
	// server listening port number
	public static final int PORT = 10000;
	// process id mapping
	public static final int PROCESS_ID[] = new int[] { 5554, 5556, 5558, 5560, 5562 };
	public static final int redirect_port[] = new int[] { 11108, 11112, 11116, 11120, 11124 };
	
	/*
	 * define db related info
	 */
	public static final String AUTHORITY = "cse586.pro0.yinyan.provider.MessageProvider";
	public static final String PATH = "messages";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH);

	public static final String DATABASE_NAME = "messages.db";
	public static final int DATABSE_VERSION = 1;

	public static final String CONTENT_TYPE_ARTICLES_LIST = "vnd.android.cursor.dir/vnd.cse586.messages";
	public static final String CONTENT_TYPE_ARTICLE_ONE = "vnd.android.cursor.item/vnd.cse586.messages";
	
	public class MessageTable implements BaseColumns {
		private MessageTable() {
		}

		public static final String MSG_TABLE = "msg_table";
		public static final String COL_ID = "_id";
		public static final String COL_PROVIDER_KEY = "provider_key";
		public static final String COL_PROVIDER_VALUE = "provider_value";
	}

	// ///////////////////////////////////////////////////////

	public static String findIPAddress(String s) {
		Pattern pattern = Pattern
				.compile("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
		Matcher matcher = pattern.matcher(s);
		return matcher.find() ? matcher.group() : null;
	}

	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
					.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
						.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						Log.d(TAG, inetAddress.getHostAddress().toString());
						// Formatter.formatIpAddress(inetAddress.getHostAddress());
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(TAG, ex.toString());
		}
		return null;
	}

	public static String getAddressBySocket() {
		try {
			// Socket socket = new Socket("8.8.8.8", 53);
			InetAddress addr = InetAddress.getByAddress("8.8.8.8", null);
			int port = 53;
			// Socket remoteAddr = new Socket("8.8.8.8", 53);
			SocketAddress sockaddr = new InetSocketAddress(addr, port);
			Socket socket = new Socket();
			socket.connect(sockaddr, 50000);
			return socket.getLocalAddress().toString();
		} catch (Exception e) {
			Log.i("", e.getMessage());
		}
		return null;
	}
}
